package cn.huolabao.controller;

import cn.huolabao.common.core.utils.R;
import cn.huolabao.model.SysUser;
import cn.huolabao.service.SysUserService;
import cn.huolabao.vo.portal.PageVo;
import cn.huolabao.vo.system.UserVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "用户管理")
public class UserController {

    private final SysUserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的条数", required = true),
            @ApiImplicitParam(name = "phone", value = "用户的手机号码"),
            @ApiImplicitParam(name = "username", value = "用户的全名称"),
            @ApiImplicitParam(name = "status", value = "用户的状态"),
    })
    @PreAuthorize("hasAnyAuthority('sys:user:view')")
    public R list(@ApiIgnore SysUser sysUser, PageVo pageVo) {
        return R.ok(userService.selectUserList(new Page<>(pageVo.getCurrent(), pageVo.getPageSize()), sysUser));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加用户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户的全名称", required = true),
            @ApiImplicitParam(name = "phone", value = "用户的手机号码", required = true),
            @ApiImplicitParam(name = "email", value = "用户的邮箱"),
            @ApiImplicitParam(name = "jobId", value = "用户的岗位ID", required = true),
            @ApiImplicitParam(name = "roleId", value = "用户的角色ID", required = true),
            @ApiImplicitParam(name = "deptId", value = "用户的部门ID", required = true),
            @ApiImplicitParam(name = "sex", value = "用户的性别"),
            @ApiImplicitParam(name = "status", value = "用户的状态", required = true),
    })
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    public R add(@ApiIgnore @RequestBody UserVo userVo) {
        log.info("add_user:{}", userVo);
        return R.ok(userService.addUser(userVo));
    }

    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的id", required = true),
            @ApiImplicitParam(name = "username", value = "用户的全名称"),
            @ApiImplicitParam(name = "phone", value = "用户的手机号码"),
            @ApiImplicitParam(name = "email", value = "用户的邮箱"),
            @ApiImplicitParam(name = "jobId", value = "用户的岗位ID"),
            @ApiImplicitParam(name = "roleId", value = "用户的角色ID"),
            @ApiImplicitParam(name = "deptId", value = "用户的部门ID"),
            @ApiImplicitParam(name = "sex", value = "用户的性别"),
            @ApiImplicitParam(name = "status", value = "用户的状态"),
    })
    @ApiOperation(value = "更新用户信息", httpMethod = "POST")
    @PreAuthorize("hasAnyAuthority('sys:user:update')")
    public R update(@ApiIgnore UserVo userVo) {
        return null;
    }

}
